package cn.juque.datapipeline.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.exception.AppException;
import cn.juque.datapipeline.config.PropertiesConfig;
import cn.juque.datapipeline.entity.DatabaseInfo;
import cn.juque.datapipeline.mapper.DatabaseInfoMapper;
import cn.juque.datapipeline.service.IDatabaseInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>驱动信息</li>
 * </ul>
 * {@code @date} 2023-03-31 09:44:41
 **/
@Service("databaseInfoService")
public class DatabaseInfoServiceImpl implements IDatabaseInfoService {

    @Resource
    private PropertiesConfig propertiesConfig;

    @Resource
    private DatabaseInfoMapper databaseInfoMapper;

    /**
     * 分页查询
     *
     * @param pageInfo     pageInfo
     * @param databaseInfo databaseInfo
     * @return DataGrid
     */
    @Override
    public List<DatabaseInfo> listInfoWithPage(PageInfo pageInfo, DatabaseInfo databaseInfo) {
        Page<DatabaseInfo> page = new Page<>(pageInfo.getPage(), pageInfo.getLimit());
        List<DatabaseInfo> list = this.databaseInfoMapper.queryInfoList(page, databaseInfo);
        pageInfo.setTotal(page.getTotal());
        return list;
    }

    /**
     * 查询
     *
     * @param databaseInfo databaseInfo
     * @return List
     */
    @Override
    public List<DatabaseInfo> listInfoWithoutPage(DatabaseInfo databaseInfo) {
        return this.databaseInfoMapper.queryInfoList(databaseInfo);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return DatabaseInfo
     */
    @Override
    public DatabaseInfo detail(String id) {
        return this.databaseInfoMapper.selectById(id);
    }

    /**
     * 保存
     *
     * @param databaseInfo databaseInfo
     */
    @Override
    public void saveInfo(DatabaseInfo databaseInfo) {
        RSA rsa = SecureUtil.rsa(this.propertiesConfig.getRsaPrivateKey(), this.propertiesConfig.getRsaPublicKey());
        String rawPass = rsa.encryptBase64(databaseInfo.getPassword().getBytes(StandardCharsets.UTF_8), KeyType.PublicKey);
        databaseInfo.setPassword(rawPass);
        boolean flag = SqlHelper.retBool(this.databaseInfoMapper.insert(databaseInfo));
        if (!flag) {
            throw new AppException(MessageEnum.SAVE_FAIL);
        }
    }

    /**
     * 更新
     *
     * @param databaseInfo databaseInfo
     */
    @Override
    public void updateInfo(DatabaseInfo databaseInfo) {
        if (CharSequenceUtil.isNotEmpty(databaseInfo.getPassword())) {
            RSA rsa = SecureUtil.rsa(this.propertiesConfig.getRsaPrivateKey(), this.propertiesConfig.getRsaPublicKey());
            String rawPass = rsa.encryptBase64(databaseInfo.getPassword().getBytes(StandardCharsets.UTF_8), KeyType.PublicKey);
            databaseInfo.setPassword(rawPass);
        }
        boolean flag = SqlHelper.retBool(this.databaseInfoMapper.updateById(databaseInfo));
        if (!flag) {
            throw new AppException(MessageEnum.UPDATE_FAIL);
        }
    }

    /**
     * 删除
     *
     * @param ids 主键列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteInfo(List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        int count = this.databaseInfoMapper.deleteBatchIds(ids);
        if (count != ids.size()) {
            throw new AppException(MessageEnum.DELETE_FAIL);
        }
    }
}
